package com.example.mytransitiondemo.Tween;

import android.os.Bundle;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.Switch;
import com.example.mytransitiondemo.BaseActivity;
import com.example.mytransitiondemo.R;


// 补间动画

public class TweenActivity extends BaseActivity implements View.OnClickListener {
    private ImageView rectangle_red;  // 图片
    private Switch isXml;  // 选择开关

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tween); // 加载

        rectangle_red = (ImageView) findViewById(R.id.rectangle_red);//图片
        isXml = (Switch) findViewById(R.id.isXml);
        //给按钮
        findViewById(R.id.translate).setOnClickListener(this);
        findViewById(R.id.rotate).setOnClickListener(this);
        findViewById(R.id.scale).setOnClickListener(this);
        findViewById(R.id.alpha).setOnClickListener(this);
        findViewById(R.id.set).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.translate:
                if (isXml.isChecked()) {
                    Animation anim = AnimationUtils.loadAnimation(this, R.anim.tween_translate);
                    rectangle_red.startAnimation(anim);
                    return;
                }
                //平移动画
                TranslateAnimation translateAnimation = new TranslateAnimation(0, 100, 0, 100);
                translateAnimation.setRepeatMode(Animation.REVERSE);
                translateAnimation.setFillAfter(true);
                translateAnimation.setDuration(2000);
                translateAnimation.setRepeatCount(1);
                rectangle_red.startAnimation(translateAnimation);
                break;
            case R.id.rotate:
                if (isXml.isChecked()) {
                    Animation anim = AnimationUtils.loadAnimation(this, R.anim.tween_rotate);
                    rectangle_red.startAnimation(anim);
                    return;
                }
                //旋转动画
                RotateAnimation rotateAnimation = new RotateAnimation(0, 180, Animation.RELATIVE_TO_SELF, Animation.RELATIVE_TO_SELF);
                rotateAnimation.setRepeatMode(Animation.REVERSE);
                rotateAnimation.setFillAfter(true);
                rotateAnimation.setDuration(2000);
                rotateAnimation.setRepeatCount(1);
                rectangle_red.startAnimation(rotateAnimation);
                break;
            case R.id.scale:
                if (isXml.isChecked()) {
                    Animation anim = AnimationUtils.loadAnimation(this, R.anim.tween_scale);
                    rectangle_red.startAnimation(anim);
                    return;
                }
                //缩放动画
                ScaleAnimation scaleAnimation = new ScaleAnimation(1, 2, 1, 2, Animation.RELATIVE_TO_SELF, Animation.RELATIVE_TO_SELF);
                scaleAnimation.setRepeatMode(Animation.REVERSE);
                scaleAnimation.setFillAfter(true);
                scaleAnimation.setDuration(2000);
                scaleAnimation.setRepeatCount(1);
                rectangle_red.startAnimation(scaleAnimation);
                break;
            case R.id.alpha:
                if (isXml.isChecked()) {
                    Animation anim = AnimationUtils.loadAnimation(this, R.anim.tween_alpha);
                    rectangle_red.startAnimation(anim);
                    return;
                }
                //透明度动画
                AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0.5f);
                alphaAnimation.setRepeatMode(Animation.REVERSE);
                alphaAnimation.setFillAfter(true);
                alphaAnimation.setDuration(2000);
                alphaAnimation.setRepeatCount(1);
                rectangle_red.startAnimation(alphaAnimation);
                break;
            case R.id.set:
                if (isXml.isChecked()) {
                    Animation anim = AnimationUtils.loadAnimation(this, R.anim.tween_set);
                    rectangle_red.startAnimation(anim);
                    return;
                }
                //动画集合
                ScaleAnimation scaleAnim = new ScaleAnimation(1, 2, 1, 2, Animation.RELATIVE_TO_SELF, Animation.RELATIVE_TO_SELF);
                scaleAnim.setRepeatMode(Animation.REVERSE);
                scaleAnim.setFillAfter(true);
                scaleAnim.setDuration(2000);
                scaleAnim.setRepeatCount(1);

                AlphaAnimation alphaAnim = new AlphaAnimation(1, 0.5f);
                alphaAnim.setRepeatMode(Animation.REVERSE);
                alphaAnim.setFillAfter(true);
                alphaAnim.setDuration(2000);
                alphaAnim.setRepeatCount(1);

                AnimationSet set = new AnimationSet(true);
                set.addAnimation(scaleAnim);
                set.addAnimation(alphaAnim);
                rectangle_red.startAnimation(set);
                break;
        }
    }
}
